package bbs.utils;

import bbs.entity.enums.ResponseCodeEnum;
import bbs.exception.BusinessException;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.SocketTimeoutException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class OKHttpUtils {
    /**
     * 请求超时时间为5秒
     */
    private static final long TIME_OUT_SECONDS = 5L;

    private static Logger logger = LoggerFactory.getLogger(OKHttpUtils.class);

    private static OkHttpClient.Builder getHttpClientBuilder() {
        OkHttpClient.Builder builder = (new OkHttpClient.Builder()).followRedirects(false).retryOnConnectionFailure(false);
        builder.connectTimeout(TIME_OUT_SECONDS, TimeUnit.SECONDS).readTimeout(TIME_OUT_SECONDS, TimeUnit.SECONDS);
        return builder;
    }

    private static Request.Builder a(Map<String, String> paramMap) {
        Request.Builder builder = new Request.Builder();
        if (null != paramMap)
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                String str2, str1 = (String)entry.getKey();
                if (entry.getValue() == null) {
                    str2 = "";
                } else {
                    str2 = (String)entry.getValue();
                }
                builder.addHeader(str1, str2);
            }
        return builder;
    }

    private static FormBody.Builder b(Map<String, String> paramMap) {
        FormBody.Builder builder = new FormBody.Builder();
        if (paramMap == null)
            return builder;
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            String str2, str1 = (String)entry.getKey();
            if (entry.getValue() == null) {
                str2 = "";
            } else {
                str2 = (String)entry.getValue();
            }
            builder.add(str1, str2);
        }
        return builder;
    }

    public static String getRequest(String paramString) throws BusinessException {
        ResponseBody responseBody = null;
        try {
            OkHttpClient.Builder builder = getHttpClientBuilder();
            Request.Builder builder1 = a(null);
            OkHttpClient okHttpClient = builder.build();
            Request request = builder1.url(paramString).build();
            Response response = okHttpClient.newCall(request).execute();
            responseBody = response.body();
            return responseBody.string();
        } catch (SocketTimeoutException | java.net.ConnectException socketTimeoutException) {
            logger.error("OKhttp POST {}", paramString, socketTimeoutException);
            throw new BusinessException(ResponseCodeEnum.CODE_900);
        } catch (Exception exception) {
            logger.error("OKhttp GET 请求异常" , exception);
            return null;
        } finally {
            if (responseBody != null)
                responseBody.close();
        }
    }

    public static String postRequest(String paramString, Map<String, String> paramMap1, Map<String, String> paramMap2) throws BusinessException {
        ResponseBody responseBody = null;
        try {
            OkHttpClient.Builder builder = getHttpClientBuilder();
            Request.Builder builder1 = a(paramMap1);
            FormBody.Builder builder2 = b(paramMap2);
            OkHttpClient okHttpClient = builder.build();
            FormBody formBody = builder2.build();
            Request request = builder1.url(paramString).post((RequestBody)formBody).build();
            Response response = okHttpClient.newCall(request).execute();
            responseBody = response.body();
            String str = responseBody.string();
            return str;
        } catch (SocketTimeoutException|java.net.ConnectException socketTimeoutException) {
            logger.error("OKhttp POST {}", paramString, socketTimeoutException);
            throw new BusinessException(ResponseCodeEnum.CODE_900);
        } catch (Exception exception) {
            logger.error("OKhttp POST {}", paramString, exception);
            return null;
        } finally {
            if (responseBody != null)
                responseBody.close();
        }
    }
}
